<?php
if (!file_exists('../install.lock')) {
    echo "系统未安装";
    exit;
}

require_once 'config.php';

echo "<h1>支付系统测试页面</h1>";

// 测试数据库连接
try {
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM products");
    $result = $stmt->fetch();
    echo "<p>✅ 数据库连接正常，商品数量: " . $result['count'] . "</p>";
} catch (Exception $e) {
    echo "<p>❌ 数据库连接失败: " . $e->getMessage() . "</p>";
}

// 测试系统设置
try {
    $settings = getSettings();
    echo "<p>✅ 系统设置获取成功，标题: " . ($settings['title'] ?? '未设置') . "</p>";
} catch (Exception $e) {
    echo "<p>❌ 系统设置获取失败: " . $e->getMessage() . "</p>";
}

// 测试支付宝配置
echo "<p>支付宝配置检查:</p>";
echo "<ul>";
echo "<li>APP_ID: " . (ALIPAY_APP_ID === 'your_app_id' ? '❌ 未配置' : '✅ 已配置') . "</li>";
echo "<li>PRIVATE_KEY: " . (ALIPAY_PRIVATE_KEY === 'your_private_key' ? '❌ 未配置' : '✅ 已配置') . "</li>";
echo "<li>PUBLIC_KEY: " . (ALIPAY_PUBLIC_KEY === 'alipay_public_key' ? '❌ 未配置' : '✅ 已配置') . "</li>";
echo "<li>NOTIFY_URL: " . ALIPAY_NOTIFY_URL . "</li>";
echo "<li>RETURN_URL: " . ALIPAY_RETURN_URL . "</li>";
echo "</ul>";

// 检查必要的表是否存在
$tables = ['products', 'orders', 'kami', 'settings'];
echo "<p>数据库表检查:</p>";
echo "<ul>";
foreach ($tables as $table) {
    try {
        $stmt = $pdo->query("SHOW TABLES LIKE '$table'");
        if ($stmt->rowCount() > 0) {
            echo "<li>✅ $table 表存在</li>";
        } else {
            echo "<li>❌ $table 表不存在</li>";
        }
    } catch (Exception $e) {
        echo "<li>❌ 检查 $table 表失败: " . $e->getMessage() . "</li>";
    }
}
echo "</ul>";

// ========== 在线支付测试 ===========
if (isset($_POST['test_amount'])) {
    $amount = floatval($_POST['test_amount']);
    if ($amount > 0) {
        $order_no = generateOrderNo();
        $stmt = $pdo->prepare("INSERT INTO orders (order_no, product_id, quantity, total_amount, status, created_at) VALUES (?, 0, 1, ?, 'pending', NOW())");
        $stmt->execute([$order_no, $amount]);
        echo "<script>location.href='payment.php?order_no={$order_no}';</script>";
        exit;
    } else {
        echo "<p style='color:red'>请输入大于0的金额</p>";
    }
}
echo "<hr><h2>在线支付测试</h2>";
echo "<form method='post' style='margin-bottom:1em;'>";
echo "<label>测试金额（元）：<input type='number' name='test_amount' step='0.01' min='0.01' required></label> ";
echo "<button type='submit'>生成支付二维码</button>";
echo "</form>";

echo "<p><a href='index.php'>返回商品页面</a></p>";
?> 